package com.example.checkqufuface.Customer;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.GridView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.checkqufuface.Adapter.FreeTypeAdapter;
import com.example.checkqufuface.R;
import com.example.checkqufuface.bean.QufufindFreeCardProductSelectResultBean;

import java.util.List;

public class CustomFreeInWindow extends PopupWindow {
    private static final String TAG = "CustomPopWindow";
    private final View view;
    private Activity context;
    private BaseQuickAdapter.OnItemClickListener itemClick;
    private List<QufufindFreeCardProductSelectResultBean.DataBean> data;
    private GridView gvFree;
    public GridItemListener gridItemListener;

    public CustomFreeInWindow(Activity context, List<QufufindFreeCardProductSelectResultBean.DataBean> data ) {
        super(context);
        this.data = data;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.widget_free_in, null);//alt+ctrl+f
        this.context = context;

        initView();
        initPopWindow();
    }


    private void initView() {
        // 获取 绑定 ID

        TextView cancelTv = view.findViewById(R.id.tv_wg_qu_xiao_free);
        gvFree = view.findViewById(R.id.gv_free_select);
        FreeTypeAdapter freeTypeAdapter = new FreeTypeAdapter(context, data);
        gvFree.setAdapter(freeTypeAdapter);

        freeTypeAdapter.SetGridViewItemListener(new FreeTypeAdapter.GridViewItemListener() {
            @Override
            public void gvItem(int pos) {
                gridItemListener.GridItem(pos);
            }
        });

        cancelTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

    }

    private void initPopWindow() {
        this.setContentView(view);
        // 设置弹出窗体的宽
        this.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        // 设置弹出窗体的高
        this.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        // 设置弹出窗体可点击()
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.mypopwindow_anim_style);
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x000000);
//        //设置弹出窗体的背景
        this.setBackgroundDrawable(dw);
//        backgroundAlpha(context, 0.5f);//0.0-1.0
        this.backgroundAlpha(context, 0.5f);
    }

    /**
     * 设置添加屏幕的背景透明度(值越大,透明度越高)
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(Activity context, float bgAlpha) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        context.getWindow().setAttributes(lp);
    }

    public interface GridItemListener{
        void GridItem(int pos);
    }

    public void setGridItemListener(GridItemListener gridItemListener){
        this.gridItemListener = gridItemListener;
    }


}
